<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'

const router = useRouter()
const activeTab = ref('account')
const accountFormRef = ref()
const phoneFormRef = ref()

const selfUser = ref({
    userAccount: '',
    userPhone: '',
    userPassword: ''
})

const rules = {
    userAccount: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { max: 20, message: '用户名至多为20位', trigger: 'blur' }
    ],
    userPhone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
    ],
    userPassword: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        // { min: 6, max: 18, message: '长度6-18位', trigger: 'blur' }
    ]
}

const switchTab = (type) => {
    activeTab.value = type
    selfUser.value = {
        userAccount: '',
        userPhone: '',
        userPassword: ''
    }
    if (type === 'account') {
        accountFormRef.value?.clearValidate()
    } else {
        phoneFormRef.value?.clearValidate()
    }
}

const handleLogin = async () => {
    try {
        const currentForm = activeTab.value === 'account' ? accountFormRef.value : phoneFormRef.value
        
        await currentForm.validate()

        const loginData = activeTab.value === 'account' ? {
            userAccount: selfUser.value.userAccount,
            userPassword: selfUser.value.userPassword
        } : {
            userPhone: selfUser.value.userPhone,
            userPassword: selfUser.value.userPassword
        }

        console.log('登录数据:', loginData)
        ElMessage.success(`${activeTab.value === 'account' ? '用户名' : '手机号'}登录成功`)
        setTimeout(() => router.push({ name: 'topic-recommend' }), 800)
    } catch (error) {
        ElMessage.warning('请正确填写表单')
    }
}

const toRegister = () => {
  router.push({ name: 'register' })
}
</script>

<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>欢迎登录</h2>
                <div class="login-type-switch">
                    <el-button :type="activeTab === 'account' ? 'primary' : 'info'" @click="switchTab('account')">
                        用户名登录
                    </el-button>
                    <el-button :type="activeTab === 'phone' ? 'primary' : 'info'" @click="switchTab('phone')">
                        手机号登录
                    </el-button>
                </div>
            </div>

            <div class="login-body">
                <el-form v-show="activeTab === 'account'" ref="accountFormRef" :model="selfUser" :rules="rules" label-width="80px">
                    <el-form-item label="用户名" prop="userAccount">
                        <el-input v-model="selfUser.userAccount" placeholder="请输入用户名" clearable maxlength="20" />
                    </el-form-item>

                    <el-form-item label="密码" prop="userPassword">
                        <el-input v-model="selfUser.userPassword" type="password" placeholder="请输入密码" show-password clearable />
                    </el-form-item>
                </el-form>

                <el-form v-show="activeTab === 'phone'" ref="phoneFormRef" :model="selfUser" :rules="rules" label-width="80px">
                    <el-form-item label="手机号" prop="userPhone">
                        <el-input v-model="selfUser.userPhone" placeholder="请输入手机号" clearable maxlength="11" />
                    </el-form-item>

                    <el-form-item label="密码" prop="userPassword">
                        <el-input v-model="selfUser.userPassword" type="password" placeholder="请输入密码" show-password clearable />
                    </el-form-item>
                </el-form>

                <div class="action-buttons">
                    <el-button type="primary" class="login-btn" @click="handleLogin">
                        {{ activeTab === 'account' ? '用户名登录' : '手机号登录' }}
                    </el-button>
                    <el-button class="register-btn" @click="toRegister">
                        注册
                    </el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #f0f2f5;
}

.login-box {
    width: 400px;
    padding: 30px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    color: #333;
    margin-bottom: 20px;
}

.login-type-switch {
    display: flex;
    gap: 20px;
    justify-content: center;
}

.login-type-switch .el-button {
    width: 120px;
}

.submit-btn {
    width: 100%;
    margin-top: 20px;
    height: 40px;
}

.el-form-item {
    margin-bottom: 22px;
}

.el-input {
    width: 85%;
}

.action-buttons {
    display: block;
    margin-top: 40px;
}

.login-btn {
    display: block;
    height: 40px;
    width: 100%;
}

.register-btn {
    display: block;
    margin: 20px auto;
    height: 40px;
    width: 100%;
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #606266;
}

.register-btn:hover {
    background-color: #e4e7ed;
}
</style>